package com.koi.clubservice.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.koi.clubservice.Utils.ClassScheduleDataListener;
import com.koi.clubservice.Utils.CourseScheduleUtils;
import com.koi.clubservice.domain.po.CourseSchedule;
import com.koi.clubservice.domain.vo.CourseScheduleQueryVo;
import com.koi.clubservice.service.ICoursescheduleService;
import com.koi.mccommon.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author koi
 * @since 2025-03-27
 */
@RestController
@RequestMapping("/courseschedule")
public class CoursescheduleController {

    @Autowired
    private ICoursescheduleService coursescheduleService;



    @PostMapping("/getCourseTime")
    @ApiOperation(value = "获取课表时间")
    public Result<List<CourseSchedule>> ReadCourseInfo(@RequestBody( required = false) CourseScheduleQueryVo courseScheduleQueryVo) {
        try{
            LambdaQueryWrapper<CourseSchedule> queryWrapper = new LambdaQueryWrapper<>();
            List<CourseSchedule> courseSchedules ;

            if(courseScheduleQueryVo!=null){
                queryWrapper.like(CourseSchedule::getClassroom, courseScheduleQueryVo.getClassroom());
                queryWrapper.eq(CourseSchedule::getCampus,courseScheduleQueryVo.getCampus());
                courseSchedules = coursescheduleService.list(queryWrapper);//取得数据
            }
            else{
                courseSchedules = coursescheduleService.list(null);
            }

            for (CourseSchedule courseSchedule : courseSchedules) {
                String week = courseSchedule.getWeek();
                String period = courseSchedule.getClassTime();
                // 调用工具类计算时间范围
                List<CourseScheduleUtils.DateRange> dateRanges = CourseScheduleUtils.convertToTime(week, period);
                // 将计算得到的时间范围设置到 CourseSchedule 对象中
                courseSchedule.setDateRanges(dateRanges);
            }


            return Result.success(courseSchedules);
        }
        catch (Exception e){
            e.printStackTrace();
            return Result.error("读取课表失败");
        }
    }





    public void ReadClassSchedule() {
        // 替换为你的 Excel 文件路径
        String filePath = "D:\\MyClub\\MyClub\\club-service\\src\\main\\resources\\xlxFilm\\课表.xls";
        File file = new File(filePath);

        ClassScheduleDataListener listener = new ClassScheduleDataListener();
        // 读取 Excel 文件
        EasyExcel.read(file, CourseSchedule.class, listener).sheet().doRead();

        List<CourseSchedule> dataList = listener.getDataList();

        // 读取的数据存入mysql
        coursescheduleService.saveBatch(dataList);

        // 打印读取的数据
        for (CourseSchedule data : dataList) {
            System.out.println(data.toString());
        }
    }

}
